/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-shadow */
/* eslint-disable no-param-reassign */
/* eslint global-require: "error" */

// all these variations of require() are ok

import { setToken } from '@/utils/auth';

const avatarUrl = require('../../assets/image/avatar.jpg');

const state = {
  token: '',
  name: '',
  avatar: '',
  roles: [],
};


const actions = {
  // 登陆
  login({ commit }) {
    return new Promise((reslove, reject) => {
      try {
        const token = 'aaaaaa';
        commit('SET_TOKEN', token);
        setToken(token);
        reslove();
      } catch {
        reject();
      }
    });
  },
  // 获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      try {
        // 写死角色
        const roles = ['tourist'] || null;
        const name = 'tourist';
        commit('SET_ROLES', roles);
        commit('SET_NAME', name);
        commit('SET_AVATAR', avatarUrl);
        console.log('getInfo=roles=', roles);
        resolve(roles);
      } catch {
        const a = 'err';
        reject(a);
      }
    });
  },
};

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
